<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">

                    <?php if (!in_array('img', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['img']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="img" id="img_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="image_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('img1', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['img1']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="img1" id="img1_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="image1_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('video', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['video']?></label>
                        <div class="layui-input-block">
                            <video src="" autoplay="true" controls="true" width="100"></video>
                            <input type="text" style="display:none" name="video" id="video_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="video_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传视频
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('active_img', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['active_img']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="active_img" id="active_img_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="active_image_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('title', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['title']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('name', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['name']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>



                    <?php if (!in_array('img_url', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['img_url']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="img_url" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('desc', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['desc']?></label>
                        <div class="layui-input-block">
                            <textarea name="desc" id="desc" cols="30" class="layui-textarea" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_name', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_name']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="en_name" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_title', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_title']?></label>
                        <div class="layui-input-block">
                            <input type="text" name="en_title" value="" class="layui-input">
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_desc', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_desc']?></label>
                        <div class="layui-input-block">
                            <textarea name="en_desc" id="en_desc" class="layui-textarea" cols="30" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('content', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['content']?></label>
                        <div class="layui-input-block">
                            <textarea name="content" id="content" cols="30" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('en_content', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['en_content']?></label>
                        <div class="layui-input-block">
                            <textarea name="en_content" id="en_content" cols="30" rows="10"></textarea>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('icon', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['icon']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="icon" id="icon_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="icon_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>

                    <?php if (!in_array('active_icon', $hidden_fields)) : ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['active_icon']?></label>
                        <div class="layui-input-block">
                            <img class="img-3" src="" />
                            <input type="text" style="display:none" name="active_icon" id="active_icon_url" value="" />
                            <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="active_icon_upload" permission="app.admin.upload.avatar">
                                <i class="layui-icon layui-icon-upload"></i>上传图片
                            </button>
                        </div>
                    </div>
                    <?php endif ?>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label"><?=$fields_name['sort']?></label>
                        <div class="layui-input-block">
                            <input type="number" name="sort" value="" class="layui-input">
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
    <div class="button-container">
        <input type="hidden" name="id" value="<?=$id?>">
        <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit lay-filter="save">
            提交
        </button>
        <button type="reset" class="pear-btn pear-btn-md">
            重置
        </button>
    </div>
</div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/page-content/select" + location.search;
            const UPDATE_API = "/app/admin/page-content/update";
            
            let editor, editor2; // Declare editor variables globally

            // 图片上传逻辑
            let $ = layui.jquery;

            // 清空图片和下一个元素的值
            $(document).on("click", ".img-3", function () {
                $(this).attr("src", ""); // 清空当前图片的 src
                $(this).next("input").val(""); // 清空下一个元素的值
            });

            // 获取数据库记录
            layui.use(["form", "util", "popup", "upload", "tinymce"], function () {
                let $ = layui.$;
                let form = layui.form;
                let tinymce = layui.tinymce;
                
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                            }
                        });

                        // Image upload for "图片"
                        let imgInput = $("#image_upload").prev();
                        imgInput.prev().attr("src", imgInput.val());
                        layui.upload.render({
                            elem: "#image_upload",
                            url: "/app/admin/upload/image",
                            acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                            done: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                            }
                        });

                        // Image upload for "动态选中后的图片"
                        let activeImgInput = $("#active_image_upload").prev();
                        activeImgInput.prev().attr("src", activeImgInput.val());
                        layui.upload.render({
                            elem: "#active_image_upload",
                            url: "/app/admin/upload/image",
                            acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                            done: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                            }
                        });

                        let iconInput = $("#icon_upload").prev();
                        iconInput.prev().attr("src", iconInput.val());
                        layui.upload.render({
                            elem: "#icon_upload",
                            url: "/app/admin/upload/image",
                            acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                            done: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                            }
                        });

                        let activeIconInput = $("#active_icon_upload").prev();
                        activeIconInput.prev().attr("src", activeIconInput.val());
                        layui.upload.render({
                            elem: "#active_icon_upload",
                            url: "/app/admin/upload/image",
                            acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                            done: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                            }
                        });

                        let img1Input = $("#image1_upload").prev();
                        img1Input.prev().attr("src", img1Input.val());
                        layui.upload.render({
                            elem: "#image1_upload",
                            url: "/app/admin/upload/image",
                            acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                            done: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                            }
                        });

                        let videoInput = $("#video_upload").prev();
                        videoInput.prev().attr("src", videoInput.val());
                        layui.upload.render({
                            elem: "#video_upload",
                            url: "/app/admin/upload/file",
                            acceptMime: "video/mp4",
                            accept: "file",
                            done: function (res) {
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                            }
                        });

                        // Initialize TinyMCE editors
                        if ($("#content").length > 0) {
                            editor = tinymce.render({
                                elem: "#content",
                                images_upload_url: "/app/admin/upload/image",
                                height: 400
                            });
                        }
                        
                        if ($("#en_content").length > 0) {
                            editor2 = tinymce.render({
                                elem: "#en_content",
                                images_upload_url: "/app/admin/upload/image",
                                height: 400
                            });
                        }
                        
                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                        layui.form.render();
                    }
                });
                
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                
                // 表单提交
                form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    
                    // Add TinyMCE content to form data if editors exist
                    if (editor) {
                        data.field['content'] = editor.getContent();
                    }
                    
                    if (editor2) {
                        data.field['en_content'] = editor2.getContent();
                    }
                    
                    $.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dataType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false; // Prevent traditional form submission
                });
            });
        </script>

    </body>

</html>
